<?php
/*
// @author : lael
// blog : http://hi.baidu.com/lael80
// http://www.gzyd.net
// all right reserved.
*/

namespace ydsdk\controllers;

class install {
	public $db = null;
	public $smarty = null;
	public function __construct(){
		if(file_exists(YD_PATH.'data/install/install.lock'))redirect('con=admin/index');

		if(!$GLOBALS['YD_SDK']['libraries']['smarty']){
			$this->smarty = $this->library('smarty', array(
				'cache_lifetime' =>  30*24*3600,
				'caching' =>  false,
				'use_sub_dirs' =>  false,
				'left_delimiter' =>  '{',
				'right_delimiter' =>  '}',
			));
			$GLOBALS['YD_SDK']['libraries']['smarty'] = $this->smarty;
		}else{
			$this->smarty = $GLOBALS['YD_SDK']['libraries']['smarty'];
		}
		if(!is_dir(YD_ROOT.'data/runtime/install/views'))@mkdir(YD_ROOT.'data/runtime/install/views', 0777, true);
		if(!is_dir(YD_ROOT.'data/runtime/install/cache'))@mkdir(YD_ROOT.'data/runtime/install/views', 0777, true);
        $this->smarty->template_dir    = YD_ROOT.'theme/install/views';
        $this->smarty->compile_dir     = YD_ROOT.'data/runtime/install/views';
        $this->smarty->cache_dir       = YD_ROOT.'data/runtime/install/cache';
		
		$this->smarty->assign('theme', YD_URL.'theme/install/static');
	}

	public function index(){
		if(issubmit()){
			if(!$GLOBALS['YD_SDK']['libraries']['mysqli']){
				$this->db = $this->library('mysqli', array(
					'debug' => 1,
					'host' => $_POST['host'],
					'user' => $_POST['user'],
					'pwd' => $_POST['pwd'],
					'dbname' => $_POST['createdb'] ? $_POST['dbname'] : false,
					'prefix' => $_POST['prefix'],
					'timeout' => 2,
				));
				$GLOBALS['YD_SDK']['libraries']['mysqli'] = $this->db;
			}else{
				$this->db = $GLOBALS['YD_SDK']['libraries']['mysqli'];
			}
			$error = $this->db->error();
			if($error)exitjson(-1, '数据库信息错误, '.$error['errno'].' '.$error['error']);			
			
			$sql = file_get_contents(YD_PATH.'data/install/install.sql');
			foreach($_POST as $k => $v){
				$sql = str_ireplace('{$'.$k.'}', $v, $sql);
			}
			$sql = str_ireplace('{$time}', time(), $sql);
			$sql = str_ireplace('{$createdb}', '', $sql);

			//执行sql
			$this->db->querys($sql);
			$error = $this->db->error();
			if($error)exitjson(-1, '数据库信息错误, '.$error['errno'].' '.$error['error']);
			//配置文件
			$config = file_get_contents(YD_PATH.'data/install/config.php');
			foreach($_POST as $k => $v){
				$config = str_ireplace('{$'.$k.'}', $v, $config);
			}
			file_put_contents(YD_PATH.'config/config.php', $config);
			file_put_contents(YD_PATH.'data/install/install.lock', formattime(time()));
			exitjson(1, '系统安装成功');	
		}
		$this->smarty->display('index.html');
	}
	
	public function model($name = '', $params = array()){
		if(!$GLOBALS['YD_SDK']['models'][$name]){
			include_once(YD_ROOT.'models/'.$name.'.php');
			$class = '\ydsdk\models\\'.$name;
			$GLOBALS['YD_SDK']['models'][$name] = new $class($params);
		}
		return $GLOBALS['YD_SDK']['models'][$name];
	}
	
	public function library($name = '', $params = array()){
		if(!$GLOBALS['YD_SDK']['libraries'][$name]){
			include_once(YD_ROOT.'libraries/'.$name.'.php');
			$class = '\ydsdk\libraries\\'.$name;
			$GLOBALS['YD_SDK']['libraries'][$name] = new $class($params);
		}
		return $GLOBALS['YD_SDK']['libraries'][$name];
	}
}
